<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Maven多模块项目架构指南 | 技术小馆</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .hero {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .drop-cap:first-letter {
            font-size: 3.5em;
            float: left;
            line-height: 0.9;
            margin: 0.1em 0.1em 0 0;
            color: #4f46e5;
            font-weight: 700;
        }
        .code-block {
            background-color: #2d3748;
            border-left: 4px solid #4f46e5;
        }
        .module-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .hover-grow:hover {
            transform: scale(1.03);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 md:py-32">
        <div class="container mx-auto max-w-6xl">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold mb-4 leading-tight">Maven多模块项目<br>架构设计指南</h1>
                    <p class="text-xl opacity-90 mb-8">构建可维护、可扩展的Java企业级应用架构</p>
                    <div class="flex space-x-4">
                        <a href="#why" class="bg-white text-indigo-600 px-6 py-3 rounded-lg font-medium hover:bg-gray-100 transition duration-300">为什么需要</a>
                        <a href="#structure" class="bg-indigo-700 text-white px-6 py-3 rounded-lg font-medium hover:bg-indigo-800 transition duration-300">项目结构</a>
                    </div>
                </div>
                <div class="md:w-1/2">
                    <div class="bg-white bg-opacity-10 rounded-2xl p-6 backdrop-filter backdrop-blur-sm">
                        <div class="mermaid">
                            graph TD
                            A[父模块] --> B[公共模块]
                            A --> C[数据访问层]
                            A --> D[业务逻辑层]
                            A --> E[Web层]
                            B --> C
                            B --> D
                            C --> D
                            D --> E
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-6xl px-4 py-12">
        <!-- Why Section -->
        <section id="why" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-12 h-1 bg-indigo-500 mr-4"></div>
                <h2 class="text-3xl font-bold text-gray-800">为什么需要多模块项目？</h2>
            </div>
            
            <p class="text-lg mb-6 drop-cap">在现代Java开发中，Maven已经成为构建和管理项目的标准工具之一。随着项目规模的扩大，单一模块的项目结构往往无法满足复杂业务的需求。因此，多模块项目（Multi-Module Project）成为了许多大型项目的首选。</p>
            
            <div class="grid md:grid-cols-2 gap-8 mt-10">
                <div class="bg-white p-6 rounded-xl shadow-md hover-grow transition duration-300">
                    <div class="text-indigo-500 text-2xl mb-4">
                        <i class="fas fa-code-branch"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">代码复用性差</h3>
                    <p class="text-gray-600">不同功能模块之间的代码无法有效复用，导致重复开发和维护成本增加。</p>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md hover-grow transition duration-300">
                    <div class="text-indigo-500 text-2xl mb-4">
                        <i class="fas fa-tachometer-alt"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">构建效率低</h3>
                    <p class="text-gray-600">每次构建都需要编译整个项目，即使只修改了其中一小部分代码，浪费大量时间。</p>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md hover-grow transition duration-300">
                    <div class="text-indigo-500 text-2xl mb-4">
                        <i class="fas fa-project-diagram"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">依赖管理复杂</h3>
                    <p class="text-gray-600">随着依赖的增加，单一模块的依赖管理会变得混乱，容易出现版本冲突。</p>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md hover-grow transition duration-300">
                    <div class="text-indigo-500 text-2xl mb-4">
                        <i class="fas fa-users"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">团队协作困难</h3>
                    <p class="text-gray-600">多个团队或开发者在同一个模块中工作，容易产生代码冲突和协调问题。</p>
                </div>
            </div>
            
            <p class="mt-10 text-lg bg-indigo-50 p-6 rounded-lg border-l-4 border-indigo-500">
                多模块项目通过将项目拆分为多个独立的模块，每个模块负责特定的功能或业务逻辑，从而解决上述问题。这种架构模式在大型企业应用中尤为重要。
            </p>
        </section>

        <!-- Structure Section -->
        <section id="structure" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-12 h-1 bg-indigo-500 mr-4"></div>
                <h2 class="text-3xl font-bold text-gray-800">Maven多模块项目的基本结构</h2>
            </div>
            
            <p class="text-lg mb-8">一个典型的Maven多模块项目通常包含一个父模块（Parent Module）和多个子模块（Child Module）。父模块负责管理子模块的公共配置，而子模块则负责具体的业务逻辑。</p>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="module-card bg-white p-6 rounded-xl shadow-md transition duration-300">
                    <h3 class="text-2xl font-bold mb-4 text-indigo-700 flex items-center">
                        <i class="fas fa-layer-group mr-3"></i> 父模块
                    </h3>
                    <p class="mb-4">父模块的<code>pom.xml</code>文件中定义了项目的公共配置，如依赖管理、插件配置、属性定义等。父模块本身通常不包含任何源代码，它只是一个容器，用于管理子模块。</p>
                    
                    <div class="code-block text-sm text-gray-200 p-4 rounded-lg overflow-x-auto">
                        <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;com.ts&lt;/groupId&gt;
    &lt;artifactId&gt;ts-parent&lt;/artifactId&gt;
    &lt;version&gt;1.0.0&lt;/version&gt;
    &lt;packaging&gt;pom&lt;/packaging&gt;
    &lt;modules&gt;
        &lt;module&gt;ts-common&lt;/module&gt;
        &lt;module&gt;ts-service&lt;/module&gt;
        &lt;module&gt;ts-web&lt;/module&gt;
    &lt;/modules&gt;
    &lt;dependencyManagement&gt;
        &lt;dependencies&gt;
            &lt;!-- 依赖管理配置 --&gt;
        &lt;/dependencies&gt;
    &lt;/dependencyManagement&gt;
&lt;/project&gt;</code></pre>
                    </div>
                </div>
                
                <div class="module-card bg-white p-6 rounded-xl shadow-md transition duration-300">
                    <h3 class="text-2xl font-bold mb-4 text-indigo-700 flex items-center">
                        <i class="fas fa-cube mr-3"></i> 子模块
                    </h3>
                    <p class="mb-4">子模块是实际包含源代码和资源的模块。每个子模块都有自己的<code>pom.xml</code>文件，但它们会继承父模块的配置。子模块可以是独立的库、服务、Web应用等。</p>
                    
                    <div class="code-block text-sm text-gray-200 p-4 rounded-lg overflow-x-auto">
                        <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;parent&gt;
        &lt;groupId&gt;com.ts&lt;/groupId&gt;
        &lt;artifactId&gt;ts-parent&lt;/artifactId&gt;
        &lt;version&gt;1.0.0&lt;/version&gt;
    &lt;/parent&gt;
    &lt;artifactId&gt;ts-common&lt;/artifactId&gt;
    &lt;dependencies&gt;
        &lt;!-- 模块特定依赖 --&gt;
    &lt;/dependencies&gt;
&lt;/project&gt;</code></pre>
                    </div>
                </div>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-6">
                    <h3 class="text-2xl font-bold mb-4 text-indigo-700">典型的多模块项目结构</h3>
                    <div class="flex flex-col md:flex-row items-center">
                        <div class="md:w-1/2 mb-6 md:mb-0">
                            <div class="mermaid">
                                graph TD
                                P[父模块 ts-parent] --> C[公共模块 ts-common]
                                P --> D[数据访问层 ts-dao]
                                P --> S[业务逻辑层 ts-service]
                                P --> W[Web层 ts-web]
                                C --> D
                                C --> S
                                D --> S
                                S --> W
                            </div>
                        </div>
                        <div class="md:w-1/2 pl-6">
                            <ul class="space-y-3 text-gray-700">
                                <li class="flex items-start">
                                    <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                    <span><strong>ts-common</strong>：包含通用工具类、常量定义等</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                    <span><strong>ts-dao</strong>：负责数据访问层，包含数据库操作相关的代码</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                    <span><strong>ts-service</strong>：负责业务逻辑层，包含核心业务逻辑</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                    <span><strong>ts-web</strong>：负责Web层，包含控制器、视图等</span>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Best Practices -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-12 h-1 bg-indigo-500 mr-4"></div>
                <h2 class="text-3xl font-bold text-gray-800">多模块项目的最佳实践</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-12">
                <div>
                    <h3 class="text-2xl font-bold mb-4 text-indigo-700">模块划分原则</h3>
                    <div class="space-y-6">
                        <div class="flex items-start">
                            <div class="bg-indigo-100 rounded-full p-2 mr-4">
                                <i class="fas fa-sitemap text-indigo-600"></i>
                            </div>
                            <div>
                                <h4 class="font-bold text-lg mb-1">按功能划分</h4>
                                <p class="text-gray-600">将不同功能的代码放在不同的模块中。例如，公共模块包含通用工具类，服务模块包含业务逻辑，Web模块包含Web层代码。</p>
                            </div>
                        </div>
                        
                        <div class="flex items-start">
                            <div class="bg-indigo-100 rounded-full p-2 mr-4">
                                <i class="fas fa-layer-group text-indigo-600"></i>
                            </div>
                            <div>
                                <h4 class="font-bold text-lg mb-1">按层次划分</h4>
                                <p class="text-gray-600">将不同层次的代码放在不同的模块中。例如，数据访问层模块负责数据库操作，业务逻辑层模块负责业务处理，Web层模块负责用户界面。</p>
                            </div>
                        </div>
                        
                        <div class="flex items-start">
                            <div class="bg-indigo-100 rounded-full p-2 mr-4">
                                <i class="fas fa-users text-indigo-600"></i>
                            </div>
                            <div>
                                <h4 class="font-bold text-lg mb-1">按团队划分</h4>
                                <p class="text-gray-600">如果项目由多个团队开发，可以按团队划分模块。每个团队负责一个或多个模块的开发，减少代码冲突。</p>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div>
                    <h3 class="text-2xl font-bold mb-4 text-indigo-700">关键配置要点</h3>
                    <div class="space-y-6">
                        <div class="flex items-start">
                            <div class="bg-indigo-100 rounded-full p-2 mr-4">
                                <i class="fas fa-link text-indigo-600"></i>
                            </div>
                            <div>
                                <h4 class="font-bold text-lg mb-1">依赖管理</h4>
                                <p class="text-gray-600">使用<code>dependencyManagement</code>在父模块中统一管理依赖版本，子模块继承这些配置，避免版本冲突。</p>
                            </div>
                        </div>
                        
                        <div class="flex items-start">
                            <div class="bg-indigo-100 rounded-full p-2 mr-4">
                                <i class="fas fa-code-branch text-indigo-600"></i>
                            </div>
                            <div>
                                <h4 class="font-bold text-lg mb-1">构建顺序</h4>
                                <p class="text-gray-600">Maven会根据模块依赖关系自动确定构建顺序。使用<code>mvn clean install</code>命令构建整个项目。</p>
                            </div>
                        </div>
                        
                        <div class="flex items-start">
                            <div class="bg-indigo-100 rounded-full p-2 mr-4">
                                <i class="fas fa-tags text-indigo-600"></i>
                            </div>
                            <div>
                                <h4 class="font-bold text-lg mb-1">版本管理</h4>
                                <p class="text-gray-600">所有模块的版本号应保持一致。在父模块中定义版本号，子模块继承父模块的版本号。</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-6 md:mb-0">
                    <h3 class="text-xl font-bold text-white mb-2">技术小馆</h3>
                    <p class="opacity-75">分享高质量技术内容</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-indigo-400 hover:text-white transition duration-300 flex items-center">
                        <i class="fas fa-external-link-alt mr-2"></i> http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>